package com.kcyu.egoodsmapbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kcyu.egoodsmapbackend.entity.Category;
import com.kcyu.egoodsmapbackend.entity.FileDO;
import com.kcyu.egoodsmapbackend.mapper.CategoryMapper;
import com.kcyu.egoodsmapbackend.service.FileService;
import com.kcyu.egoodsmapbackend.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kcyu
 * @since 2025-02-28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private FileService fileService;

    @Override
    public List<Category> getFirstLevelCategory() {
        List<Category> categories = this.getBaseMapper().selectList(new QueryWrapper<Category>()
                .lambda()
                .eq(Category::getLevel, 0)
        );
        for (Category category : categories) {
            String coverPic = category.getCoverPic();
            List<FileDO> coverPics = fileService.queryFile("MA001", coverPic);
            if (!coverPics.isEmpty()) {
                category.setCoverPicUrl(coverPics.get(0).getFilePath());
            }
        }

        return categories;
    }
}
